# 导入Pin、PWM和random库.
from machine import Pin, PWM
import math

# rgb 灯控制
# 使用 rgb = RgbLed(21,19,18)
# rgb.setColor(255,0,0)
class RgbLed:
    # r g b IO 引脚
    def __init__(self,r,g,b):
        self.r = PWM(Pin(r),freq=1000, duty=0)  
        self.g = PWM(Pin(g),freq=1000, duty=0)  
        self.b = PWM(Pin(b),freq=1000, duty=0)  
    # r g b 0-255,像ps那样的颜色范围
    def setColor(self,r, g, b):
        self.r.duty(math.ceil(r*4.011))
        self.g.duty(math.ceil(g*4.011))
        self.b.duty(math.ceil(b*4.011))
    def clear(self):
        self.r.duty(0)
        self.g.duty(0)
        self.b.duty(0)